package cn.village.controller;

import cn.village.entity.DataDictionary;
import cn.village.entity.LeaseContract;
import cn.village.service.DataDictionaryService;
import cn.village.service.LeaseContractService;
import cn.village.utils.PageSupport;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (LeaseContract)表控制层
 *
 * @author makejava
 * @since 2021-03-03 11:49:18
 */
@Controller
@RequestMapping("/leaseContract")
public class LeaseContractController {
    /**
     * 服务对象
     */
    @Resource
    private LeaseContractService leaseContractService;
    @Resource
    private DataDictionaryService dataDictionaryService;

    /**
     * 分页查询租赁合同列表
     * @param model
     * @param pageIndex
     * @param contractNO
     * @return
     * @throws Exception
     */
    @RequestMapping("/list")  //TODO 解决时间显示格式问题，配合前端完成页码跳转功能
    public String list(Model model, Integer pageIndex, Integer contractNO) throws Exception {
        //判断是否第一次进入页面，如果第一次进入页面则设置pageIndex为1
        if (pageIndex == null || pageIndex <= 0){
            pageIndex = 1;
        };

        PageSupport pageSupport = new PageSupport();
        //设置当前页码
        pageSupport.setCurrentPageNo(pageIndex);
        //设置页面容量
        pageSupport.setPageSize(5);

        //查询所有租赁合同的总数
        Map map = new HashMap();
        Integer leaseContractCountByMap = leaseContractService.getLeaseContractCountByMap(map);
        //设置总记录数
        pageSupport.setTotalCount(leaseContractCountByMap);

        //查询租赁合同列表
        Map params = new HashMap();
        //设置查询条件
        params.put("contractNO", contractNO);
        Integer beginPos = (pageSupport.getCurrentPageNo()-1)*pageSupport.getPageSize();
        params.put("beginPos",beginPos);
        params.put("pageSize", pageSupport.getPageSize());
        List leaseContractListByMap = leaseContractService.getLeaseContractListByMap(params);

        //返回前端需要的数据
        model.addAttribute("totalCount", leaseContractCountByMap);
        model.addAttribute("totalPageNo",pageSupport.getTotalPageCount());
        model.addAttribute("currentPageNo", pageSupport.getCurrentPageNo());
        model.addAttribute("lease",leaseContractListByMap);

        //跳转到租赁合同详情列表页
        return "/contract-lease-list";
    }

    /**
     * 根据id查询租赁合同详情
     * @param model
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping("/lease_details")
    public String lease_details(Model model, @RequestParam String id) throws Exception {
        //根据id查询合同详情信息
        LeaseContract lease = leaseContractService.getLeaseContractById(id);
        //把合同详情信息返回给前端
        model.addAttribute("lease",lease);
        //跳转到租赁合同详情页
        return "lease-details";
    }

    /**
     * 编辑租赁合同详情信息
     * @param model
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping("/goupdata")
    public String goupdata(Model model, @RequestParam String id) throws Exception {
        //根据id查询合同详情信息
        LeaseContract lease = leaseContractService.getLeaseContractById(id);
        //查询数据字典中的付款方式
        Map map = new HashMap();
        map.put("typeCode","PAYMENT_TERM");
        List<DataDictionary> paymentTermList= dataDictionaryService.getDataDictionaryListByMap(map);
        model.addAttribute("paymentTermList",paymentTermList);
        //查询租金交付情况
        Map params = new HashMap();
        params.put("typeCode","PAYMENT_STATUS");
        List<DataDictionary> pamentStatusList = dataDictionaryService.getDataDictionaryListByMap(params);
        model.addAttribute("pamentStatusList",pamentStatusList);
        //把合同详情信息返回给前端
        model.addAttribute("lease",lease);
        //跳转到租赁合同更新页
        return "lease-update";
    }

    /**
     * 添加编辑后的租赁合同信息到数据库，并重定向到租赁合同列表
     * @param id
     * @param contractNO
     * @param contractInfo
     * @param sigDate
     * @param keepDate
     * @param contractors
     * @param contractName
     * @param leaseStartDate
     * @param leaseEndDate
     * @param rental
     * @param rentalPurpose
     * @param paymentTerm
     * @param paymentStatus
     * @param remark
     * @return
     * @throws Exception
     */
    @RequestMapping("lease_update")  //TODO 重定向到租赁合同页面后没有数据显示
    public String lease_update(@RequestParam(required = true) Integer id,
                               @RequestParam(required = true) Integer contractNO,
                               @RequestParam(required = true) String contractInfo,
                               @RequestParam(required = false) Date sigDate,
                               @RequestParam(required = true) Date keepDate,
                               @RequestParam(required = false) String contractors,
                               @RequestParam(required = true) String contractName,
                               @RequestParam(required = true) Date leaseStartDate,
                               @RequestParam(required = true) Date leaseEndDate,
                               @RequestParam(required = true) BigDecimal rental,
                               @RequestParam(required = true) String rentalPurpose,
                               @RequestParam(required = true) Integer paymentTerm,
                               @RequestParam(required = true) Integer paymentStatus,
                               @RequestParam(required = false) String remark) throws Exception {
        LeaseContract leaseContract = new LeaseContract();
        leaseContract.setId(id);
        leaseContract.setContractNO(contractNO);
        leaseContract.setContractInfo(contractInfo);
        leaseContract.setSigDate(sigDate);
        leaseContract.setKeepDate(keepDate);
        leaseContract.setContractors(contractors);
        leaseContract.setContractName(contractName);
        leaseContract.setLeaseStartDate(leaseStartDate);
        leaseContract.setLeaseEndDate(leaseEndDate);
        leaseContract.setRental(rental);
        leaseContract.setRentalPurpose(rentalPurpose);
        leaseContract.setPaymentTerm(paymentTerm);
        leaseContract.setPaymentStatus(paymentStatus);
        leaseContract.setRemark(remark);
        leaseContract.setModifyDate(new Date());
        leaseContractService.qdtxModifyLeaseContract(leaseContract);
        return "redirect:/contract-lease-list.jsp";
    }

    @RequestMapping("/goadd")
    public String goadd(Model model) throws Exception {
        //查询数据字典中的付款方式
        Map map = new HashMap();
        map.put("typeCode","PAYMENT_TERM");
        List<DataDictionary> paymentTermList= dataDictionaryService.getDataDictionaryListByMap(map);
        model.addAttribute("paymentTermList",paymentTermList);
        //查询租金交付情况
        Map params = new HashMap();
        params.put("typeCode","PAYMENT_STATUS");
        List<DataDictionary> pamentStatusList = dataDictionaryService.getDataDictionaryListByMap(params);
        model.addAttribute("pamentStatusList",pamentStatusList);
        return "lease-add";
    }

    /**
     * 新增合同信息，并重定向到租赁合同列表
     * @param contractNO
     * @param contractInfo
//     * @param sigDate
     * @param keepDate
     * @param contractors
     * @param contractName
     * @param leaseStartDate
     * @param leaseEndDate
     * @param rental
     * @param rentalPurpose
     * @param paymentTerm
     * @param paymentStatus
     * @param remark
     * @return
     * @throws Exception
     */
    @RequestMapping("/lease_add")  //TODO 前端需对日期格式进行校验。sigDate可以为空，如果用户不填该如何处理？
                                   //TODO 需要对合同编号进行校验，合同编号不允许重复
    public String lease_add(@RequestParam(required = true) Integer contractNO,
                            @RequestParam(required = true) String contractInfo,
                            @RequestParam(required = false) Date sigDate,
                            @RequestParam(required = true) Date keepDate,
                            @RequestParam(required = false) String contractors,
                            @RequestParam(required = true) String contractName,
                            @RequestParam(required = true) Date leaseStartDate,
                            @RequestParam(required = true) Date leaseEndDate,
                            @RequestParam(required = true) BigDecimal rental,
                            @RequestParam(required = true) String rentalPurpose,
                            @RequestParam(required = true) Integer paymentTerm,
                            @RequestParam(required = true) Integer paymentStatus,
                            @RequestParam(required = false) String remark) throws Exception {
        LeaseContract leaseContract = new LeaseContract();
        System.out.println("签订日期"+sigDate);
        leaseContract.setContractNO(contractNO);
        leaseContract.setContractInfo(contractInfo);
        leaseContract.setSigDate(sigDate);
        leaseContract.setKeepDate(keepDate);
        leaseContract.setContractors(contractors);
        leaseContract.setContractName(contractName);
        leaseContract.setLeaseStartDate(leaseStartDate);
        leaseContract.setLeaseEndDate(leaseEndDate);
        leaseContract.setRental(rental);
        leaseContract.setRentalPurpose(rentalPurpose);
        leaseContract.setPaymentTerm(paymentTerm);
        leaseContract.setPaymentStatus(paymentStatus);
        leaseContract.setRemark(remark);
        leaseContract.setCreationDate(new Date());
        leaseContract.setModifyBy("lin");
        leaseContractService.qdtxAddLeaseContract(leaseContract);
        return "redirect:/contract-lease-list.jsp";
    }

    /**
     * 根据id删除租赁合同信息，并重定向到租赁合同列表
     * @param model
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping("/del")  //TODO 重定向到租赁合同页面后数据没有更新
    public String del(Model model, @RequestParam String id) throws Exception {
        Integer result = leaseContractService.qdtxDeleteLeaseContractById(id);
        model.addAttribute("delResult",result);
        return "redirect:/contract-lease-list.jsp";
    }


}